extends Node2D

#@export var enemy_scene: PackedScene
@export var spawn_interval = 0.1
@export var spawn_boss = 30
const enemy_scene = preload("res://Scenes/Enemy.tscn")
const enemy_fast_scene = preload("res://Scenes/EnemyFast.tscn")
const enemy_shooter_scene = preload("res://Scenes/EnemyShooter.tscn")
const enemy_boss1_scene = preload("res://Scenes/EnemyBoss1.tscn")
const enemy_boss2_scene = preload("res://Scenes/EnemyBoss2.tscn")

func _ready():
	# 根据关卡设置生成间隔
	spawn_interval = $"../LevelManager".get_current_spawn_interval()
	$Timer.wait_time = spawn_interval
	$Timer.start()
	
	# 根据关卡设置Boss生成时间
	var boss_config = $"../LevelManager".get_current_boss_config()
	$TimerBoss.wait_time = boss_config.spawn_time
	$TimerBoss.start()
	
	# 设置背景
	$"../Backgroud".texture = $"../LevelManager".get_current_background()

func _on_timer_timeout():
	spawn_enemy()
	
	# 检查是否达到关卡完成条件
	if $"../LevelManager".check_level_completion($"..".score):
		# 停止敌人生成
		$Timer.stop()
		$TimerBoss.stop()
		
		# 显示关卡完成消息
		var level_complete_label = Label.new()
		level_complete_label.text = "Level " + str($"../LevelManager".current_level) + " Complete!"
		level_complete_label.add_theme_font_size_override("font_size", 40)
		level_complete_label.position = Vector2(120, 400)
		get_parent().add_child(level_complete_label)
		
		# 5秒后进入下一关，给玩家足够时间看到关卡完成信息
		await get_tree().create_timer(5).timeout
		level_complete_label.queue_free()
		
		if $"../LevelManager".next_level():
			# 更新生成间隔
			spawn_interval = $"../LevelManager".get_current_spawn_interval()
			$Timer.wait_time = spawn_interval
			
			# 更新Boss生成时间
			var boss_config = $"../LevelManager".get_current_boss_config()
			$TimerBoss.wait_time = boss_config.spawn_time
			
			# 更新背景
			$"../Backgroud".texture = $"../LevelManager".get_current_background()
			
			# 显示新关卡信息
			var new_level_label = Label.new()
			new_level_label.text = "Starting Level " + str($"../LevelManager".current_level)
			new_level_label.add_theme_font_size_override("font_size", 40)
			new_level_label.position = Vector2(120, 400)
			get_parent().add_child(new_level_label)
			
			# 3秒后开始新关卡
			await get_tree().create_timer(3).timeout
			new_level_label.queue_free()
			
			# 重新启动敌人生成
			$Timer.start()
			$TimerBoss.start()
		else:
			# 游戏通关
			$"../win".visible = true
			await get_tree().create_timer(3).timeout
			get_tree().change_scene_to_file("res://main_menu.tscn")

func spawn_enemy():
	# 根据当前关卡配置决定生成哪种敌人
	var enemy_config = $"../LevelManager".get_current_enemy_config()
	var rand = randf()
	var enemy
	
	if rand < enemy_config.normal:
		enemy = enemy_scene.instantiate()
	elif rand < enemy_config.normal + enemy_config.fast:
		enemy = enemy_fast_scene.instantiate()
	else:
		enemy = enemy_shooter_scene.instantiate()
	
	# 随机生成位置（屏幕顶部）
	var viewport_size = get_viewport_rect().size
	var spawn_x = randf_range(120, viewport_size.x - 120)  # 横向随机
	var spawn_y = 100  # 从屏幕顶部生成
	
	enemy.position = Vector2(spawn_x, spawn_y)
	
	get_parent().add_child(enemy)
	
func spawn_Boss1():
	var enemy = enemy_boss1_scene.instantiate()
	# 随机生成位置（屏幕顶部）
	var viewport_size = get_viewport_rect().size
	var spawn_x = randf_range(250, viewport_size.x - 250)  # 横向随机
	var spawn_y = 100  # 从屏幕顶部生成
	
	enemy.position = Vector2(spawn_x, spawn_y)
	get_parent().add_child(enemy)


func _on_diff_up_timeout() -> void:
	pass # Replace with function body.


func _on_timer_boss_timeout() -> void:
	# 根据当前关卡配置决定生成哪种Boss
	var boss_config = $"../LevelManager".get_current_boss_config()
	var boss = boss_config.scene.instantiate()
	
	# 随机生成位置（屏幕顶部中央）
	var viewport_size = get_viewport_rect().size
	var spawn_x = viewport_size.x / 2  # 屏幕中央
	var spawn_y = 100  # 从屏幕顶部生成
	
	boss.position = Vector2(spawn_x, spawn_y)
	get_parent().add_child(boss)
